﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace MeizuSDK.Core
{
    /// <summary>
    /// 电话通讯类
    /// </summary>
    public static class Telephony
    {
        #region 常量
        private const uint RIL_NCLASS_ALL = 0x00ff0000;
        private const uint RIL_CALLTYPE_VOICE = 1;
        private const uint RIL_DIALOPT_PRESENTID = 2;
        #endregion

        #region DllImport
        [DllImport("ril.dll")]
        private static extern int RIL_Initialize(uint dwIndex, ResultCallback pfnResult, NotifyCallback pfnNotify, uint dwNotificationClasses, uint dwParam, ref IntPtr lphRil);

        [DllImport("ril.dll")]
        private static extern int RIL_Dial(IntPtr hRil, byte[] lpszAddress, uint dwType, uint dwOptions);

        [DllImport("ril.dll")]
        private static extern int RIL_Deinitialize(IntPtr hRil);
        #endregion

        private static readonly ResultCallback pfnResult = OnResult;

        private static readonly NotifyCallback pfnNotify = OnNotify;

        private delegate void ResultCallback(uint dwCode, int hrCmdID, System.IntPtr lpData, uint cbData, uint dwParam);

        private delegate void NotifyCallback(uint dwCode, System.IntPtr lpData, uint cbData, uint dwParam);

        /// <summary>
        /// 给指定号码发送短消息
        /// </summary>
        /// <param name="phoneNumber">接收短信的手机号码</param>
        /// <param name="text">短信内容</param>
        public static void SendSMSMessage(string phoneNumber, string text)
        {
            Process.Start("smsui.exe", string.Format("-n {0}▓{1}", phoneNumber, text));
        }
        /// <summary>
        /// 拨打指定的电话号码
        /// </summary>
        /// <param name="phoneNumber">待拨打的电话号码</param>
        public static void MakePhoneCall(string phoneNumber)
        {
            IntPtr lphRil = IntPtr.Zero;

            RIL_Initialize(1, pfnResult, pfnNotify, RIL_NCLASS_ALL, 0, ref lphRil);

            int hr = RIL_Dial(lphRil, Encoding.ASCII.GetBytes(phoneNumber), RIL_CALLTYPE_VOICE, RIL_DIALOPT_PRESENTID);

            RIL_Deinitialize(lphRil);
        }

        private static void OnResult(uint dwCode, int hrCmdID, System.IntPtr lpData, uint cbData, uint dwParam)
        {
        }

        private static void OnNotify(uint dwCode, System.IntPtr lpData, uint cbData, uint dwParam)
        {
        }

    }
}
